<?php
	//include("Library.php");
	class GetnewsRegex
	{
		var $id;
		var $IndexRegex;
		var $ContentRegex;
		var $url;
	}
	class GetNewsService
	{
		static function GetHTML($url)
		{
			$html=file_get_contents($url);
			$html=preg_replace('/\s+/'," ",$html);
			return $html;
		}
		static function GetFileName($url)
		{
			$filename=$url;
			$pos=strrpos($filename,"/");
			if($pos>0)
				$filename=substr($filename,$pos+1);
			return $filename;
		}
		
		static function get_regex($regex)
		{
			$regex=str_replace("/","\/",$regex);
			return "/".$regex."/";
		}
		static function do_reg($text, $regex,$group)
		{
			$regex=GetNewsService::get_regex($regex);
			preg_match_all($regex, $text, $result);	
			return $result[$group];
		}
		static function GetDomain($url)
		{
			$regex="(?<domain>http://.*?)/";
			$result=GetNewsService::do_reg($url,$regex,"domain");
			$domain=$result[0];
			return $domain;
		}
		static function get_content($file,$regex,$group)		{
			$result=GetNewsService::do_reg($file,$regex,$group);	
			return $result;			
		}
		static  function GetImage($content)	//Lay link anh
		{
			$regex='src="(?<linkimage>.*?)"';
			$regex=GetNewsService::get_regex($regex);
			$content=preg_replace('/\s+/'," ",$content);
			preg_match_all($regex,$content,$result);
			return $result['linkimage'];
		}
		static function getnew($gn)
		{
			
			$count=0;
			$arr=array();
			$id=$gn->id;
			$IndexRegex=GetNewsService::get_regex($gn->IndexRegex);
			$ContentRegex=GetNewsService::get_regex($gn->ContentRegex);
			$url=$gn->url;
			$file=file_get_contents($url);
			$file = mb_convert_encoding($file, 'HTML-ENTITIES', "UTF-8"); 
			$file=preg_replace('/\s+/'," ",$file);
			preg_match_all($IndexRegex,$file,$result);
			
			$count=count($result['title']);
			$domain=GetNewsService::GetDomain($url);
			for($i=0;$i<$count;$i++)
			{
				try
				{
				$domainRegex=GetNewsService::get_regex($domain);
				$arr[$i]['title']=$result['title'][$i];
				$head=$result['head'][$i];
				$head=GetNews::GetstandLinkHead($domain,$head);
				$arr[$i]['head']=$head;
				if(!preg_match($domainRegex,$result['linkimage'][$i]))
					$arr[$i]['linkimage']=$domain.$result['linkimage'][$i];
				else 
					$arr[$i]['linkimage']=$result['linkimage'][$i];
				$linkcontent=$result['linkcontent'][$i];					
				if(!preg_match($domainRegex,$linkcontent))
					$linkcontent=$domain.$linkcontent;
					
				$file=file_get_contents($linkcontent);
				$file = mb_convert_encoding($file, 'HTML-ENTITIES', "UTF-8"); 
				$file=preg_replace('/\s+/'," ",$file);
					
				preg_match($ContentRegex,$file,$RsContent);
				$content=$RsContent['content'];	
				$dirimage=NV_UPLOADS_DIR.'/news/';
				$content=GetNews::GetstandLinkImage($domain,$content);
				$arr[$i]['content']=$content;
				$imagecontent=GetNewsService::GetImage($content);
				}
				catch(Exception $e)
				{
				}
			}

			return $arr;
			
		}
	}
?>